//
//  ChooseTableViewCell.m
//  ImagePicker
//
//  Created by csh on 16/7/25.
//  Copyright © 2016年 com.read. All rights reserved.
//

#import "ChooseTableViewCell.h"
@interface ChooseTableViewCell()
@property(nonatomic,strong)UIImageView *iconView;
@property(nonatomic,strong)UILabel *desLabel;
@end
@implementation ChooseTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        
    }
    
    return self;
}
#pragma mark UI
-(UIImageView *)iconView{
    if (!_iconView) {
        _iconView=[[UIImageView alloc]init];
        _iconView.clipsToBounds=YES;
        [self.contentView addSubview:_iconView];
    }
    return _iconView;
}
-(UILabel *)desLabel{
    if (!_desLabel) {
        _desLabel=[[UILabel alloc]init];
        [self.contentView addSubview:_desLabel];
    }
    return _desLabel;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    self.iconView.frame=CGRectMake(15, 5, 50, 50);
    CGFloat desX=CGRectGetMaxX(self.iconView.frame)+15;
    self.desLabel.frame=CGRectMake(desX, 5, self.frame.size.width-desX-5,50);
}
- (void)setCollection:(PHAssetCollection *)collection
{
    _collection = collection;
    PHFetchOptions *option = [[PHFetchOptions alloc] init];
    option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
    PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:collection options:option];
    self.desLabel.text=[NSString stringWithFormat:@"%@ (%lu)",collection.localizedTitle,(unsigned long)result.count];
    if(result.count>0)
    {
        PHAsset *asset = [result objectAtIndex:0];
        [[DVImagePickerManager manager] getPhotoWithAsset:asset completion:^(UIImage *image, NSDictionary *info, BOOL isDegraded) {
            self.iconView.image = image;
        }];
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
